package com.xframe.common.core.aop;

import com.xframe.common.config.DecorationConfig;
import com.xframe.xdal.core.util.XReflect;
/**
 * AOP装饰器基础基类
 *
 * @author xufeng
 * @version 1.0
 * @date 2022-09-28
 */
public abstract class BaseDecoration implements IMethodDecoration{


    protected Integer execOrder;
    protected BaseDecoration decoration = null;
    protected String args;

    public BaseDecoration getDecoration() {
        return decoration;
    }

    public void setDecoration(BaseDecoration decoration) {
        this.decoration = decoration;
    }
    /**
     * 测试是否满足执行的条件
     * @param context 运行时对象参数
     */
    @Override
    public abstract boolean testMethod(XAOPContext context);
    /**
     * 初始化
     * @param context 运行时对象参数
     */
    @Override
    public void init(XAOPContext context) {
        if(decoration != null) {
            decoration.init(context);
        }

    }
    /**
     * 目标方法执行前
     * @param context 运行时对象参数
     */
    @Override
    public void beforeExecution(XAOPContext context) {
        if(decoration != null) {
            decoration.beforeExecution(context);
        }
    }
    /**
     * 目标方法执行后(异常不会调用)
     * @param context 运行时对象参数
     */
    @Override
    public void afterExecution(XAOPContext context) {
        if(decoration != null) {
            decoration.afterExecution(context);
        }
    }
    /**
     * 目标方法执行成功(异常不会调用)
     * @param context 运行时对象参数
     */
    @Override
    public void finallyComplete(XAOPContext context) {
        if(decoration != null) {
            decoration.finallyComplete(context);
        }
    }
    /**
     * 目标方法执行失败(成功不会调用)
     * @param context 运行时对象参数
     */
    @Override
    public void exceptionHandler(XAOPContext context){
        if(decoration != null) {
            decoration.exceptionHandler(context);
        }
    }
    /**
     * 获得排序值
     */
    @Override
    public Integer getExecOrder(){
        return this.execOrder;
    }
    /**
     * 设置排序值
     */
    @Override
    public void setExecOrder(Integer execOrder){
        this.execOrder = execOrder;
    }
    /**
     * 获得附加参数
     */
    @Override
    public String getArgs(){
        return this.args;
    }
    /**
     * 设置附加参数
     */
    @Override
    public void setArgs(String args){
        this.args = args;
    }

    @Override
    public int compareTo(IMethodDecoration decoration) {
        return this.getExecOrder() - decoration.getExecOrder();
    }

    /**
     * 静态工厂方法通过配置创建BaseDecoration子类的对象
     */
    public static BaseDecoration create(DecorationConfig decorationConfig){
        try {
            Class<?> dataType = XReflect.getClassByName(decorationConfig.getDataType());
            if(dataType == null){
                return null;
            }
            if(!BaseDecoration.class.isAssignableFrom(dataType)){
                return null;
            }
            BaseDecoration decoration = (BaseDecoration)dataType.newInstance();
            decoration.setExecOrder(decoration.getExecOrder());
            decoration.setArgs(decoration.getArgs());
            return decoration;
        } catch (Exception exception){
            exception.printStackTrace();
            return null;
        }

    }
    /**
     * 创建新对象(克隆)
     */
    public abstract BaseDecoration buildeNew();


}
